﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Areas/Manage/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<RuiJing.Web.Areas.Manage.Models.ProductModels.ProductEditModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	<%="edit".Equals(Model.Action,StringComparison.CurrentCultureIgnoreCase) ? "编辑" : "新增"%>产品
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div>
    <% var edit = "edit".Equals(Model.Action, StringComparison.CurrentCultureIgnoreCase); %>
    <% Html.BeginForm(Model.Action, "product", new { id="productform", enctype = "multipart/form-data" }, new { ID = edit ? (object)Model.ID : (object)Model.TypeID }); %>
    <fieldset>
        <legend><%= edit ? "编辑" : "新增"%>产品</legend>
        <%=Html.ActionLink(">>返回", "index", new { ID = Model.TypeID }) %>
        <%=Html.ValidationSummary() %>
        <ul class="editpanel">
            <li class="edititem">
                <div class="label">分类名称：</div>
                <div class="edititemcontent">
                    <span id="TypeName"><%=Model.TypeName_Ch %><%=string.IsNullOrEmpty(Model.TypeName_En) ? "" : "(" + Model.TypeName_En + ")"%></span>
                    <%=Html.HiddenFor(p=>p.TypeID) %>
                    <a href="javascript:;" onclick="ChooseType()" class=" a1">选择分类</a>
                </div>
            </li>
            <li class="edititem">
                <label for="Number" class="label">产品编号：</label>
                <div class="edititemcontent">
                    <%=Html.TextBoxFor(p=>p.Number) %>
                    <%=Html.ValidationMessageFor(p=>p.Number) %>
                </div>
            </li>
            <li class="edititem">
                <label for="Number" class="label">点击次数：</label>
                <div class="edititemcontent"><%=Model.ClickCount %></div>
            </li>
            <li class="hr">产品信息(中文)：</li>
            <li class="edititem">
                <label for="Name_Ch" class="label">产品名称：</label>
                <div class="edititemcontent">
                    <%=Html.TextBoxFor(p=>p.Name_Ch) %>
                    <%=Html.ValidationMessageFor(p=>p.Name_Ch) %>
                </div>
            </li>
            <li class="edititem">
                <div class="label">其它信息：</div>
                <div class="edititemcontent">
                    <div class="editsubitem" id="disc_Ch">
                        <% if (Model.Disc_Ch != null)%>
                        <% { %>
                        <% for (int i = 0; i < Model.Disc_Ch.Length; i++) %>
                        <% { %>
                        <div class="editsubitem" id="discitem">
                            <div class="floatleft">
                                <%=Html.TextBoxFor(p => p.Disc_Ch[i].Key)%>:
                                <%=Html.TextBoxFor(p => p.Disc_Ch[i].Value, new { @class = "text300" })%>
                            </div>
                            <a href="javascript:;" class="abtn a2 floatright" onclick="delDisc(this,'Ch')" title="删除说明">X</a>
                        </div>
                        <% } %>
                        <% } %>
                    </div>
                    <div class="editsubitem">
                        <a href="javascript:;" class="abtn a2 floatright" onclick="addDisc('Ch')" title="添加中文说明">添加</a>
                    </div>
                </div>
            </li>
            <li class="hr">产品信息(英文)：</li>
            <li class="edititem">
                <label for="Name_En" class="label">产品名称：</label>
                <div class="edititemcontent">
                    <%=Html.TextBoxFor(p=>p.Name_En) %>
                    <%=Html.ValidationMessageFor(p=>p.Name_En) %>
                </div>
            </li>
            <li class="edititem">
                <div class="label">其它信息：</div>
                <div class="edititemcontent">
                    <div class="editsubitem" id="disc_En">
                        <% if (Model.Disc_En != null)%>
                        <% { %>
                        <% for (int i = 0; i < Model.Disc_En.Length; i++) %>
                        <% { %>
                        <div class="editsubitem" id="discitem">
                            <div class="floatleft">
                                <%=Html.TextBoxFor(p=>p.Disc_En[i].Key) %>:
                                <%=Html.TextBoxFor(p => p.Disc_En[i].Value, new { @class="text300"})%>
                            </div>
                            <a href="javascript:;" class="abtn a2 floatright" onclick="delDisc(this,'En')" title="删除说明">X</a>
                        </div>
                        <% } %>
                        <% } %>
                    </div>
                    <div class="editsubitem">
                        <a href="javascript:;" class="abtn a2 floatright" onclick="addDisc('En')" title="添加英文说明">添加</a>
                    </div>
                </div>
            </li>
            <li class="hr">
                产品图片：
            </li>
            <% foreach (var t in Model.PictureTypes) %>
            <% { %>
            <li class="edititem">
                <div class="label" title='<%=t.Name_En %>'><%=t.Name_Ch %>：</div>
                <div class="edititemcontent pics" id="pics">
                    <div class='editsubitem pics_<%=t.ID %>' id="pics_<%=t.ID %>">
                    <% if (Model.Pictures != null)%>
                    <% { %>
                    <% var pics = Model.Pictures.Where(p => p.TypeID == t.ID); %>
                    <% foreach (var pic in pics) %>
                    <% { %>
                    <% var i = Array.FindIndex(Model.Pictures, p => p.ID == pic.ID); %>
                        <div class="picitem" id="picitem">
                            <div class="picimg">
                                <a href='<%=pic.FileName %>' target="_blank"><img class="img" src='<%=pic.FileName %>' alt='' /></a>
                            </div>
                            <a href='javascript:;' class='<%=(pic.IsDefault ?? false) ? "isdefault" : "" %>' onclick='setDefaultPic(this)'>默认</a>&nbsp;
                            <a href='javascript:;' onclick='delPicItem(this)'>删除</a> 
                            <%=Html.HiddenFor(p=>p.Pictures[i].ID) %>
                            <%=Html.HiddenFor(p=>p.Pictures[i].IsDefault) %>
                            <%=Html.HiddenFor(p=>p.Pictures[i].IsDel) %>
                            <%=Html.HiddenFor(p=>p.Pictures[i].FileName) %>
                            <%=Html.HiddenFor(p=>p.Pictures[i].TypeID) %>
                        </div>
                    <% } %>
                    <% } %>
                    </div>
                    <div class="editsubitem">
                        <a href='javascript:;' class="abtn a2 floatright" onclick='addPic(<%=t.ID %>)' title='添加<%=t.Name_Ch %>(<%=t.Name_En %>)图片'>添加</a>
                    </div>
                </div>
            </li>
            <% } %>
            <li class="edititem btnpanel">
                <input type="submit" value="确定保存" />
            </li>
        </ul>
    </fieldset>
    <%=Html.HiddenFor(p => p.Action)%>
    <% Html.EndForm(); %>
</div>

<% Action<IEnumerable<ProductType>,ProductType> PutTree = null; PutTree=((d,n) =>%>
<% { %>
    <% if (n != null) %>
    <% { %>
    <div class="trre_item-head">
        <span class="trre-icon trre-plus"></span>
        <span class="trre-in">
            <%=n.Name_Ch%>(<%=n.Name_En%>)
            <input type="hidden" id="hidden_id" value='<%=n.ID %>'/>
        </span>
    </div>
    <% }%>
    <% else %>
    <% { %>
    <div class="trre-content">
    <% } %>
    <ul class="trre-group">
    <%
    int? id = n == null ? null as int? : n.ID;
    var items = d.Where(p => p.ParentID == id);
    foreach (var item in items)
    {
    %>
        <li class="trre-item">
            <% PutTree(d,item); %>
        </li>
    <%
    }
    %>
    </ul>
    <% if (n != null) %>
    <% { %>
    <% }%>
    <% else %>
    <% { %>
    </div>
    <% } %>
<% }); %>
<div class="dialog">
<div class="trre-set">
    <% PutTree(Model.Types,null); %>
</div>
</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="StypeAndScript" runat="server">
    <script src='<%=Url.Content("~/Scripts/tool/trre-view.js") %>' type="text/javascript"></script>
    <script src="/Scripts/tool/upimg.js" type="text/javascript"></script>
    <link href='<%=Url.Content("~/Scripts/tool/ui.css") %>'  rel="stylesheet" type="text/css" />
    <style type="text/css">
    .typepath{ width:100%; line-height:30px; }
    .producttop{ width:100%; line-height:30px; }
    .typechilds { border-bottom:1px solid #888;}
    .typechilds,.typechilds ul{ height:auto; overflow:hidden; width:100%; }
    .typechilds li{ height:25px; padding:3px 5px; width:100px; text-align:center; float:left;}
    .pruductlist{width:100%; height:auto; overflow:hidden;}
    .pager{ width:100%; float:left; text-align:center;}
    .text300{width:300px;}
    .isdefault{color:Blue;}
    .isdel{color:Blue;}
    .trre-content
    {
        border:1px solid #c3c3c3;
        margin:10px;
        padding:10px;
        line-height:25px;
    }
    .trre-set
    {
        width:500px;
    }
    
    .dialog{ display:none;}
    </style>
<script type="text/javascript">

    $(document).ready(function () {
        $("#productform input[type='submit']").click(function () {
            var type = $("#TypeID");
            if (type.val().length > 0)
                return true;
            alert("请先选择产品所属的分类");
            return false;
        });
    })

    function ChooseType() {
        $.blockUI({ message: null });
        var g =
            {
                closeText: '取消',
                title: '选择分类',
                width: '500px',
                height: 'auto',
                beforeClose: function () {
                    $.unblockUI();
                    $('.pager').unblock();
                },
                buttons: {
                    "取消": function () {
                        $(this).dialog('close');
                    },
                    "确定": function () {
                        var item = $(".trre-content .trre-state-selected");
                        var typeid = item.find("#hidden_id").val();
                        if (isNaN(typeid)) {
                            alert("请选择所属分类！")
                            return;
                        }
                        $("#TypeID").val(typeid);
                        $("#TypeName").text(item.text());
                        $(this).dialog('close');
                    }
                }
            };
        $(".dialog").dialog(g);
    }
    function addDisc(type) {
        var discs = $("#disc_" + type);
        var count = discs.find("#discitem").length;
        var str = "" +
"                        <div class=\"editsubitem\" id=\"discitem\">" +
"                            <div class=\"floatleft\"><input type=\"text\" id=\"Disc_" + type + "_" + count + "__Key\" name=\"Disc_" + type + "[" + count + "].Key\" value />:" +
"                            <input type=\"text\" id=\"Disc_" + type + "_" + count + "__Value\" name=\"Disc_" + type + "[" + count + "].Value\" class=\"text300\" value /></div>" +
"                            <a href=\"javascript:;\" class=\"abtn a2 floatright\" onclick=\"delDisc(this,'Ch')\"  title=\"删除说明\">X</a>"+
"                        </div>"+
            "";
        $(str).appendTo(discs);
    }
    function delDisc(btn, type) {
        $(btn).parent().remove();
        upDicIndex(type);
    }

    function upDicIndex(type) {
        var discs = $(".disc_" + type + " .discitem");
        discs.each(function (i) {
            var item = $(this);
            item.find('input').each(function () {
                this.id = this.id.replace(/_\d__/, "_" + i + "__");
                this.name = this.name.replace(/\[\d\]\./, "[" + i + "].");
            });
        });
    }

    function addPic(type) {
        var pics = $(".pics_" + type);
        var count = $(".pics .picitem").length;
        var def = count == 0;
        var str = "" +
"                        <div class=\"editsubitem\" id=\"picitem\">" +
"                            <input type=\"file\" style=\"width:220px;\" onchange='ckFile(this)' size=\"30px\" id=\"Pictures_" + count + "__File\" name=\"Pictures[" + count + "].File\"/>" +
"                            <a href='javascript:;' onclick='setDefaultPic(this)' class='" + (def ? "isdefault" : "") + " abtn floatright'>默认</a>&nbsp;&nbsp;<a href='javascript:;' class=\"abtn floatright\" onclick='delPicItem(this)'>删除</a>" +
"                            <input type=\"hidden\" id=\"Pictures_" + count + "__TypeID\" name=\"Pictures[" + count + "].TypeID\" value=\"" + type + "\"/>" +
"                            <input type=\"hidden\" id=\"Pictures_" + count + "__IsDefault\" name=\"Pictures[" + count + "].IsDefault\" value=\"" + (def ? "True" : "False") + "\"/>" +
"                            <input type=\"hidden\" id=\"Pictures_" + count + "__FileName\" name=\"Pictures[" + count + "].FileName\" value=\"\"/>" +
"                            <input type=\"hidden\" id=\"Pictures_" + count + "__IsNew\" name=\"Pictures[" + count + "].IsNew\" value=\"True\"/>" +
"                        </div>";
        var d = $(str);
        d.find(".picimg a").hide();
        d.appendTo(pics);
    }

    function upPicIndex() {
        var discs = $(".pics .picitem");
        discs.each(function (i) {
            var item = $(this);
            item.find('input').each(function () {
                this.id = this.id.replace(/_\d__/, "_" + i + "__");
                this.name = this.name.replace(/\[\d\]\./, "[" + i + "].");
            });
        });
    }

    function ckFile(f) { 
        var pic=f.value;
        if(pic.length>0){
            var ext = pic.substring(pic.lastIndexOf(".") + 1).toUpperCase();
            if (ext == "JPG" || ext == "BMP" || ext == "PNG" || ext == "GIF") {
                $(f).parent().find("input[name$='FileName']").val(ext);
                //$(f).parent().find('.picimg a').show().find("img")[0].src = pic;
                return true;
            }
            alert("请选择jpg、bmp、png或gif格式的图片！");
        }

        return false;
    }

    function setDefaultPic(b) {
        $("#pics #picitem input[name$='IsDefault']").val("False");
        $("#pics #picitem .isdefault").removeClass("isdefault");
        $(b).addClass("isdefault").parent().find("input[name$='IsDefault']").val("True");
    }

    function delPicItem(b) {
        var picitem = $(b).parent();
        var isnew=picitem.find("input[type='file']").length > 0;
        if (isnew) {
            picitem.remove();
            upPicIndex();
        } else {
            if ($(b).is('.isdel')) {
                picitem.find("input[name$='IsDel']").val("False");
                $(b).removeClass("isdel");

            } else {
                picitem.find("input[name$='IsDel']").val("True");
                $(b).addClass("isdel");
            } 
        }
    }

</script>
</asp:Content>